const AnalysisApiFox = require('./AnalysisApiFox')
const path = require('path')

const express = require("express");
let app = express();
///是 express 的一个中间件 , 作用对post 请求的请求参数进行解析
const bodyParser = require("body-parser");
//配置之后会给req 添加body属性,来获取请求体
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());

app.use("/node_modules", express.static("./node_modules"));
app.engine('html', require('express-art-template'));


app.get("/func", (req, res) => {
    res.render('home.html')
});

// app.get("/web/canvaskit/canvaskit.js", (req, res) => {
//     res.render('web/canvaskit/canvaskit.js')
// });22222222222222

// app.get("/model", (req, res) => {
//     res.render('web/index.html')
// });

// app.get("/flutter.js", (req, res) => {
//     res.send('web/flutter.js')
// });

app.use(express.static(path.join(__dirname, 'views/web')));

const multer = require('multer')
const storage = multer.memoryStorage()
const upload = multer({ storage: storage })

// const router = express.Router()

app.post('/form', upload.single('html'), async (req, res, next) => {
    var buffer = req.file.buffer
    var str = buffer.toString()
    var name = req.body.name
    console.log('name '+name);
    var _res = await AnalysisApiFox.analysisContent(str, name)
    var viewModelFuncStr = await AnalysisApiFox.viewModelFunc()
    var pageLoad = await AnalysisApiFox.pageLoad()
    res.render("home.html", { 'res': _res, 'viewModelFuncStr': viewModelFuncStr, 'pageLoad': pageLoad });
})

//传过来的叫请求参数
app.post("/json", (req, res) => {
    let reqBody = req.body;
    reqBody.dateTime = "2018-11-27";
    comments.unshift(reqBody);
    //重定向 (因为配置了express才能用)
    res.redirect("/");
});

app.listen(3000);